package com.eqdd.yiqidian.ui.mine.maker.fragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.MakerVisitorBean
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonConverter
import com.eqdd.yiqidian.ui.mine.UserCardActivity
import com.eqdd.yiqidian.utils.TimeUtil
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCCoolHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.fragment_company_space.view.*
import org.jetbrains.anko.startActivity
import java.util.*

/**
 * Created by jhj on 18-8-6.
 */
class MakerVisitorFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_company_space, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val makerGuid = arguments?.getString("makerGuid")
        var pageNum = 0
        val list = arrayListOf<MakerVisitorBean>()
        val mvcHelper = MVCCoolHelper<List<MakerVisitorBean>>(view.coolRefreshView)
        view.recyclerView.layoutManager = LinearLayoutManager(activity)
        view.recyclerView.addItemDecoration(DividerLineItemDecoration(activity))
        val slimAdapterEx = SlimAdapterEx.create()
                .register<MakerVisitorBean>(R.layout.list_item_155, SlimInjector<MakerVisitorBean> { data, injector ->
                    injector?.text(R.id.tv_name, data?.staffName)
                            ?.text(R.id.tv_depart, String.format(Locale.CHINA, "%s - %s", data?.departName, data?.postName))
                            ?.text(R.id.tv_time, TimeUtil.formatWebTime(data?.createTime))
                            ?.text(R.id.tv_preview, data?.useroption)
                            ?.imageCircle(R.id.iv_photo, HttpConfig.BASE_URL + data?.iphoto)
                            ?.clicked(R.id.iv_photo) {
                                activity?.startActivity<UserCardActivity>(Config.GUID to data?.creater)
                            }

                })
                .attachTo(view.recyclerView)
                .updateData(list as List<MakerVisitorBean>?) as SlimAdapterEx<MakerVisitorBean>?
        val dataSource = ModelRx2DataSource<MakerVisitorBean>(ModelRx2DataSource.OnLoadSource<MakerVisitorBean> { page, register ->
            if (page == 1) {
                pageNum = 0
            }
            OkGo.post<HttpResult<List<MakerVisitorBean>>>(HttpConfig.BASE_URL + HttpConfig.VISITOR_GET)
                    .params("makerGuid", makerGuid)
                    .params("page", pageNum)
                    .converter(object : JsonConverter<HttpResult<List<MakerVisitorBean>>>() {
                        override fun test() {
                            super.test()
                        }
                    }).adapt(FlowableBody<HttpResult<List<MakerVisitorBean>>>())
                    .flatMap({ result ->
                        if (page == 1) {
                            mvcHelper.isAutoLoadMore = true
                        }
                        pageNum = result.page
                        Flowable.just<MutableList<MakerVisitorBean>>(if (result.items == null) ArrayList() else result.items as MutableList<MakerVisitorBean>?)
                    })


        }, 10)

        mvcHelper.setDataSource(dataSource)
        mvcHelper.adapter = slimAdapterEx
        mvcHelper.refresh()
    }
}